package com.example.a90419.audio;

import android.media.AudioRecord;
import android.media.AudioTrack;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button button_record;
    private Button button_play;
    private List<byte[]> arr = new ArrayList<>();
    private byte[] buffer = new byte[640];
    private boolean isRead = false;
    private File audioFile;
    private byte[] max= new byte[8000*5];
    private int k =0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final XFAudioRecord record2 = new XFAudioRecord();



        final XFAudioPlay audioPlay = new XFAudioPlay();

        audioPlay.play();
        record2.setListener(new MessageHandler(new MessageHandler.StreamListener() {
            @Override
            public void send(final byte[] buff) {
               // Log.e(TAG, "send: 获得了封包，封包大小为:"+buff.length);
                audioPlay.add(buff);
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                       // audioPlay.track.play();
//                      //  audioPlay.track.write(buff,0,buff.length);
//                        audioPlay.add(buff);
//                    }
//                }).start();
            }
        }));


        File fpath = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/data/files/");
        if (fpath.exists()){
            fpath.deleteOnExit();
        }
        fpath.mkdir();// 创建文件夹
        Log.e(TAG, "onCreate: 文件路径为:"+fpath);
        try {
           audioFile = File.createTempFile("recording", ".mp3", fpath);
        } catch (IOException e) {
            e.printStackTrace();
        }









        button_record = (Button)findViewById(R.id.button1);


        button_record.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
                    //按下
                    Log.e(TAG, "onTouch: 按下");

                    record2.start();


                }else if (motionEvent.getAction()==MotionEvent.ACTION_UP){
                    Log.e(TAG, "onTouch: 抬起");
                   // isRead = false;
                   // audioRecord.stop();
                    record2.end();
                    audioPlay.stop();
                   //
                }



                return false;
            }
        });



        button_play = (Button)findViewById(R.id.button2);
//        button_play.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//
//                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
//                    //按下
//                    Log.e(TAG, "onTouch: 按下");
//                }else if (motionEvent.getAction()==MotionEvent.ACTION_UP){
//                    Log.e(TAG, "onTouch: 抬起");
//                }
//
//
//
//                return false;
//            }
//        });

        button_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             //   audioTrack.play();
              //  audioTrack.write(arr.get(0),0,arr.get(0).length);
//                // 定义输入流，将音频写入到AudioTrack类中，实现播放
//                DataInputStream dis = null;
//                try {
//                    dis = new DataInputStream(
//                            new BufferedInputStream(new FileInputStream(audioFile)));
//                } catch (FileNotFoundException e) {
//                    e.printStackTrace();
//                    return;
//                }
//
//// 开始播放
//                audioTrack.play();
//// 由于AudioTrack播放的是流，所以，我们需要一边播放一边读取
//                try {
//                    while (dis.available() > 0) {
//                        int i = 0;
//                        while (dis.available() > 0 && i < buffer.length) {
//                            buffer[i] = dis.readByte();
//                            i++;
//                        }
//// 然后将数据写入到AudioTrack中
//                        audioTrack.write(buffer, 0, buffer.length);
//                    }
//                   audioTrack.stop();
//                }catch (Exception e){
//                    e.printStackTrace();
//                }

               // max = new byte[8000*100];

                audioPlay.stop();
            }

      });




    }
}
